const userDao = require('../dao/userDao')

//登录
module.exports.login = async function(phoneNum,pwd){
    let data = await userDao.findByPhone(phoneNum)
    console.log(data);
    if (data.length>0 && data[0].pwd == pwd) {
        return data[0]
    }else{
        return {}
    }
} 

//注册与后台增加用户
module.exports.reg = async function(userObj){
    return userDao.insert(userObj)
}

//注册与后台增加用户验证
module.exports.proving = async function(searchObj){
    let data = await userDao.findByUsername(searchObj)
    let isfind;
    if (data.length>0) {
        isfind = 1
        return isfind
    }else{
        isfind = 0
        return isfind
    }
}

//后台查询用户
module.exports.find = async function(searchObj,page){
    return await userDao.find(searchObj,page)
}

//后台修改用户
module.exports.change = async function(id,userObj){
    try {
        return await userDao.update(id,userObj)
    } catch (error) {
        return {}
    }
}

//后台删除用户
module.exports.delete = async function(id){
    try {
        return await userDao.delete(id)
    } catch (error) {
        return {}
    }
}

//根据ID查询用户
module.exports.findById = async function(id){
    try {
        return await userDao.findById(id)
    } catch (error) {
        return {}
    } 
}